package com.example.demo.service.impl;

import com.example.demo.dao.TestAsyncMapper;
import com.example.demo.entity.TestAsync;
import com.example.demo.service.TestService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author huangke
 * @description 多线程test
 * @date 2020/11/28
 */
@Service("testOtherService")
@Slf4j
public class TestOtherServiceImpl implements TestService {

    @Resource
    @Qualifier("testService")
    private TestService testService;

    @Resource
    private TestAsyncMapper testAsyncMapper;

    @Override
    public void testInsert() throws Exception {
        System.out.println("aaaaaaa实现了");
    }

    @Override
    public List<TestAsync> get() {
        return testAsyncMapper.selectAll();
    }

    @Transactional(rollbackFor = Exception.class)
    @Async
    public CompletableFuture<Integer> delete(int id) {
        log.info(Thread.currentThread().getName() + "===================");
        return CompletableFuture.completedFuture(testAsyncMapper.deleteByPrimaryKey(id));

    }

    public List<String> testAsync() {
        List<Integer> stringlist = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41);
        List<Integer> integers = Arrays.asList(50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,67,68,69,70,71,72,73,74,75);
        ArrayList<CompletableFuture<String>> objects = Lists.newArrayList();
        ArrayList<CompletableFuture<String>> objects1 = Lists.newArrayList();
        log.info(System.currentTimeMillis() + ""+ Thread.currentThread().getName());
        stringlist.forEach(e -> {
            try {
//                Thread.sleep(500);
                CompletableFuture<String> testaaa = testService.testaaa(e);
                objects.add(testaaa);
            } catch (Exception exception) {
                log.info(e + "出错了" + exception.getMessage());
            }
        });
        integers.forEach(e -> {
            try {
//                Thread.sleep(500);
                CompletableFuture<String> testbbb = testService.testbbb(e);
                objects1.add(testbbb);
            } catch (Exception exception) {
                log.info(e + "222出错了" + exception.getMessage());
            }
        });
        log.info(System.currentTimeMillis() + "");

        System.out.println(objects.size());
        ArrayList<String> objectsa = Lists.newArrayList();
        ArrayList<String> objectsb = Lists.newArrayList();
        objects.forEach(e -> {
            try {
                String s = e.get() + "完成";
                objectsa.add(s);
            } catch (Exception e1) {
                log.info("出错了11111" + e1.getMessage());
            }
        });
        objects1.forEach(e -> {
            try {
                String s = e.get() + "完成2222";
                objectsb.add(s);
            } catch (Exception e1) {
                log.info("出错了2222" + e1.getMessage());
            }
        });
//        try {
//            log.info("等待2秒");
//            Thread.sleep(2000);
//
//        } catch (InterruptedException e) {
//
//        }
        log.info("到这里来了");
        objectsa.addAll(objectsb);
        return objectsa;
    }


}